/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.PM;

import csse3004.GD.DA.PollMasterDA;
import csse3004.GD.Question;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author David Bullock 42062222
 */
public class liveController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private PollMasterService service = new PollMasterDA();

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        try {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            
            ModelAndView mav = new ModelAndView("live");

            mav.addObject("pid", pid);
            mav.addObject("set", true);
            mav.addObject("objects",service.getObjectHTML(pid)); //TODO David check this works
            
            return mav;
        } catch (NumberFormatException E) {
            logger.info("error!!! " + E);
            return new ModelAndView("live", "set", false);
        }
    }

    /**
     * dummy
     * Gets necessary HTML required to display the object
     * @param qid the question the object is related to
     * @return an array of strings, each containing HTML code required to display the object
     */
    public String[] getObjectHTML(int qid) {
        String[] object = new String[1];
        object[0] = "<img src=\"http://t1.gstatic.com/images?q=tbn:ANd9GcTt7IYhzpvBZbR2csgVYvH8nN9GmlXBOAVAGdPZbwo6ueNybZBW\"/>";
        return object;
    }

    /**
     * dummy
     * Gets the active question for the specified PollId. It will populate csse3004.GD.Question fully
     * @param pid Poll to populate
     * @return Populated question. Null if there is no active Question set
     */
    public Question getActiveQuestion(int pid) {
        ArrayList<Question> questions = new Question().genTestQuestion(pid);
        Question quest = questions.get(0);
        logger.info(quest.getText());
        return quest;
    }
}